<template>
    <div>
        <div class="i-layout-page-header">
            <PageHeader class="product_tabs" title="优惠券领取记录" hidden-breadcrumb></PageHeader>
        </div>
        <Card :bordered="false" dis-hover class="ivu-mt">
            <Form ref="tableFrom" :model="tableFrom"  :label-width="labelWidth" :label-position="labelPosition" @submit.native.prevent>
                <Row type="flex" :gutter="24">
                    <Col v-bind="grid">
                        <FormItem label="是否有效：" >
                            <Select  placeholder="请选择" clearable v-model="tableFrom.status" @on-change="userSearchs">
                                <Option value="1">已使用</Option>
                                <Option value="0">未使用</Option>
                                <Option value="2">已过期</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid">
                        <FormItem label="获取方式：" >
                            <Select  placeholder="请选择" clearable v-model="tableFrom.type" @on-change="userSearchs">
                                <Option value="send">后台发放</Option>
                                <Option value="get">手动领取</Option>
                                <Option value="level_send">等级发放</Option>
                                <Option value="birthday_send">生日发放</Option>
                                <Option value="consume_send">年终回馈发放</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid" v-if="tableFrom.type == 'consume_send'">
                        <FormItem label="消费级别：" >
                          <Select  placeholder="请选择" clearable v-model="tableFrom.level" @on-change="userSearchs">
                              <Option v-for="(item,index) in consumeListApi" :key="index" :value="item.grade">{{ item.name }}</Option>
                          </Select>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid">
                        <FormItem label="领取人姓名：" label-for="real_name">
                            <Input  placeholder="请输入领取人" v-model="tableFrom.real_name" clearable/>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid">
                        <FormItem label="手机号：" label-for="phone">
                            <Input search enter-button placeholder="请输入领取人" v-model="tableFrom.phone"  @on-search="userSearchs"/>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid">
                        <FormItem label="优惠券搜索：" label-for="coupon_title">
                            <Input search enter-button placeholder="请输入优惠券名称" v-model="tableFrom.coupon_title" @on-search="userSearchs"/>
                        </FormItem>
                    </Col>
                    <Col v-bind="grid">
                        <FormItem label="领取时间：" label-for="add_time">
                            <DatePicker
                                    :editable="false"
                                    @on-change="onchangeTime"
                                    :value="timeVal"
                                    format="yyyy/MM/dd"
                                    type="daterange"
                                    placement="bottom-start"
                                    placeholder="自定义时间"
                                    style="width: 200px"
                                    class="mr20"
                                    :options="options"
                            ></DatePicker>
                        </FormItem>
                   </Col>
                   <!-- <Col v-bind="grid"><Button class="ml10" @click="exports">导出</Button></Col> -->
                </Row>
            </Form>
            <Table :columns="columns1" :data="tableList">
                <template slot-scope="{ row, index }" slot="is_fail">
                    <Icon type="md-checkmark" v-if="row.is_fail === 0" color="#0092DC" size="14"/>
                    <Icon type="md-close" v-else color="#ed5565" size="14"/>
                </template>
                <template slot-scope="{ row, index }" slot="phone">
                    <div>{{ row.phone.slice(0, -3) }}***</div>
                </template>
                <template slot-scope="{ row, index }" slot="start_time">
                    <span> {{row.start_time | formatDate}}</span>
                </template>
                <template slot-scope="{ row, index }" slot="end_time">
                    <span> {{row.end_time | formatDate}}</span>
                </template>
            </Table>
            <div class="acea-row row-right page">
                <Page :total="total" :current="tableFrom.page" show-elevator show-total @on-change="pageChange"
                      :page-size="tableFrom.limit"/>
            </div>
        </Card>
    </div>
</template>

<script>
    import { mapState } from 'vuex';
    import { userListApi } from '@/api/marketing';
    import { vipListConsume } from "@/api/user";
    import { formatDate } from '@/utils/validate';
    import { storeCouponUser } from "@/api/order";
    import exportExcel from "@/utils/newToExcel.js";
    export default {
        name: 'storeCouponUser',
        filters: {
            formatDate (time) {
                if (time !== 0) {
                    let date = new Date(time * 1000);
                    return formatDate(date, 'yyyy-MM-dd hh:mm');
                }
            }
        },
        data () {
            return {
                columns1: [
                    {
                        title: 'ID',
                        key: 'id',
                        width: 80
                    },
                    {
                        title: '优惠券名称',
                        key: 'coupon_title',
                        minWidth: 130
                    },
                    {
                        title: '领取人',
                        key: 'nickname',
                        minWidth: 120
                    },
                    {
                        title: '手机号',
                        slot: 'phone',
                        minWidth: 120
                    },
                    {
                        title: '姓名',
                        key: 'real_name',
                        minWidth: 120
                    },
                    {
                        title: '面值',
                        key: 'coupon_price',
                        minWidth: 100
                    },
                    {
                        title: '最低消费额',
                        key: 'use_min_price',
                        minWidth: 120
                    },
                    {
                        title: '隶属人',
                        key: 'adviser_name',
                        minWidth: 100
                    },
                    {
                        title: '开始使用时间',
                        slot: 'start_time',
                        minWidth: 150
                    },
                    {
                        title: '结束使用时间',
                        slot: 'end_time',
                        minWidth: 150
                    },
                    {
                        title: '获取方式',
                        key: 'type',
                        minWidth: 150
                    },
                    {
                        title: '是否可用',
                        slot: 'is_fail',
                        minWidth: 120
                    },
                    {
                        title: '状态',
                        key: 'status',
                        minWidth: 110
                    },
                    {
                        title: '过期方式',
                        key: 'remark',
                        minWidth: 120
                    }
                ],
                tableList: [],
                grid: {
                    xl: 7,
                    lg: 7,
                    md: 12,
                    sm: 24,
                    xs: 24
                },
                tableFrom: {
                    status: '',
                    coupon_title: '',
                    nickname: '',
                    real_name: '',
                    phone: '',
                    type: '',
                    level: '',
                    add_time: '',
                    page: 1,
                    limit: 15
                },
                total: 0,
                consumeListApi: [],
                timeVal: [],
                options: {
                    shortcuts: [
                        {
                            text: "今天",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                new Date(
                                new Date().getFullYear(),
                                new Date().getMonth(),
                                new Date().getDate()
                                )
                            );
                            return [start, end];
                            },
                        },
                        {
                            text: "昨天",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.setTime(
                                new Date(
                                    new Date().getFullYear(),
                                    new Date().getMonth(),
                                    new Date().getDate() - 1
                                )
                                )
                            );
                            end.setTime(
                                end.setTime(
                                new Date(
                                    new Date().getFullYear(),
                                    new Date().getMonth(),
                                    new Date().getDate() - 1
                                )
                                )
                            );
                            return [start, end];
                            },
                        },
                        {
                            text: "最近7天",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.setTime(
                                new Date(
                                    new Date().getFullYear(),
                                    new Date().getMonth(),
                                    new Date().getDate() - 6
                                )
                                )
                            );
                            return [start, end];
                            },
                        },
                        {
                            text: "最近30天",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.setTime(
                                new Date(
                                    new Date().getFullYear(),
                                    new Date().getMonth(),
                                    new Date().getDate() - 29
                                )
                                )
                            );
                            return [start, end];
                            },
                        },
                        {
                            text: "本月",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.setTime(
                                new Date(new Date().getFullYear(), new Date().getMonth(), 1)
                                )
                            );
                            return [start, end];
                            },
                        },
                        {
                            text: "本年",
                            value() {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.setTime(new Date(new Date().getFullYear(), 0, 1))
                            );
                            return [start, end];
                            },
                        },
                    ],
                },
            }
        },
        computed: {
            ...mapState('admin/layout', [
                'isMobile'
            ]),
            labelWidth () {
                return this.isMobile ? undefined : 90;
            },
            labelPosition () {
                return this.isMobile ? 'top' : 'right';
            }
        },
        created () {
            this.getList();
            this.getListConsume();
        },
        methods: {
            getListConsume() {
                vipListConsume().then((res) => {
                    this.consumeListApi = res.data.list
                });
            },
            onchangeTime(e) {
                this.timeVal = e;
                this.tableFrom.add_time = this.timeVal.join("-");
                this.tableFrom.pageNum = 1;
                this.getList();
            },
            // 列表
            getList () {
                this.loading = true;
                this.tableFrom.status = this.tableFrom.status || '';
                userListApi(this.tableFrom).then(async res => {
                    let data = res.data
                    this.tableList = data.list;
                    this.total = res.data.count;
                    this.loading = false;
                }).catch(res => {
                    this.loading = false;
                    this.$Message.error(res.msg);
                })
            },
            pageChange (index) {
                this.tableFrom.page = index;
                this.getList();
            },
            // 表格搜索
            userSearchs () {
                this.tableFrom.page = 1;
                this.getList();
            },
            async exports() {
            let [th, filekey, data, fileName] = [[], [], [], ""];
            let excelData = JSON.parse(JSON.stringify(this.tableFrom));
            console.log(this.tableFrom,"1111")
            excelData.page = 1;
            excelData.product_id= this.$route.query.product_id || "";
            for (let i = 0; i < excelData.page + 1; i++) {
                let lebData = await this.getExcelData(excelData);
                if (!fileName) fileName = lebData.filename;
                if (!filekey.length) {
                filekey = lebData.filekey;
                }
                if (!th.length) th = lebData.header;
                if (lebData.export.length) {
                    data = data.concat(lebData.export);
                    excelData.page++;
                }
                // else {
                //     break; // 如果 lebData.export.length 为空，则结束循环
                // }
            }
            exportExcel(th, filekey, fileName, data);
            },
            getExcelData(excelData) {
                return new Promise((resolve, reject) => {
                    storeCouponUser(excelData).then((res) => {
                    return resolve(res.data);
                    });
                });
            },
        }
    }
</script>

<style scoped>

</style>
